//
//  NSAttributedString+Extension.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

extension NSAttributedString {

    //  根据表情模型生成表情富文本
    class func attributedStringWithEmoticon(emoticon: CZEmoticon, font: UIFont) -> NSAttributedString {
    
        //  1. 获取表情按钮的对应的表情图片
        let image = UIImage(named: emoticon.path!)
        //  2. 通过UIImage创建一个文本附件(NSTextAttachment)
        let attachment = CZTextAttachment()//NSTextAttachment()
        
        //  设置图片
        attachment.image = image
        //  设置表情模型
        attachment.emoticon = emoticon
        //  获取行高,指定大小
        let fontHeight = font.lineHeight
        //  设置图片大小
        attachment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        //  3.通过NSTextAttachment创建富文本(NSAttributedString)
        let attributedStr = NSAttributedString(attachment: attachment)
        return attributedStr
        
    }
    
    
}
